package xyz.xtt.exchange.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import xyz.xtt.common.starter.cache.CacheManager;
import xyz.xtt.exchange.constants.ExchangeCache;
import xyz.xtt.exchange.entity.TblCarouseImage;
import xyz.xtt.exchange.mapper.CarouseImageMapper;
import xyz.xtt.exchange.service.ICarouseImageService;
import xyz.xtt.exchange.vo.BannerVo;

/**
 * <p>
 * 轮播图 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-07
 */
@Service
public class CarouseImageServiceImpl extends ServiceImpl<CarouseImageMapper, TblCarouseImage> implements ICarouseImageService {
    @Autowired
    private CacheManager cacheManager;

    private static Integer StatusOk = 0;
    private static Integer HomePage = 10;

    @Override
	public List<BannerVo> getBanner() {
        // 获取 轮播图 查询缓存
        List<TblCarouseImage> imagesCache = getImagesCache();
        if (imagesCache.isEmpty()) {
            return null;
        }
        List<BannerVo> bannerVos = new ArrayList<>();
        imagesCache.forEach(image -> {
            bannerVos.add(new BannerVo(image.getFilePath(), image.getContent()));
        });
        return bannerVos;
    }


    // 查询缓存
    public List<TblCarouseImage> getImagesCache() {
        return cacheManager.cacheList(this::getImages, ExchangeCache.HOME_BANNER);
    }


    // 查询数据
    public List<TblCarouseImage>getImages() {
        String page = String.format("limit %s", HomePage);
        LambdaQueryWrapper<TblCarouseImage> query = new LambdaQueryWrapper<>();
        query.eq(TblCarouseImage::getState, StatusOk)
                .orderByDesc(TblCarouseImage::getId)
                .last(page);
        return  baseMapper.selectList(query);
    }
}
